package com.otod.cd.doc.web;


import java.util.List;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.otod.base.exception.AppException;
import com.otod.base.json.JsonSuccess;
import com.otod.cd.doc.bo.CdDocShareBo;
import com.otod.cd.doc.bo.CdDocShareFolderResutlBo;
import com.otod.cd.doc.service.CdDocFileService;
import com.otod.cd.doc.service.CdDocShareAuthService;
import com.otod.cd.doc.service.CdDocShareService;
import com.otod.cd.doc.vo.CdDocFolderVo;
import com.otod.commons.auth.SessionUserHolder;
import com.otod.commons.auth.SessionUserUtil;
import com.otod.commons.constants.CommonConstant;
import com.otod.commons.utils.StringUtils;
import com.otod.commons.web.BaseController;
import com.otod.pm.prj.service.ProjectService;
import com.otod.pm.prj.vo.ProjectVo;
import com.otod.sys.emp.mapper.EmpMapper;
import com.otod.sys.emp.service.impl.EmpServiceImpl;
import com.otod.sys.vo.AuapREmpOrgRoleVo;



@Controller
@RequestMapping("cd/Share")
public class CdDocShareContrloller extends BaseController {
	
	@Autowired	
	private CdDocShareService cdDocShareService;
	@Autowired
	private CdDocShareAuthService cdDocShareAuthService;
	@Autowired	
	private CdDocFileService cdDocFileService;
	@Autowired
	private EmpMapper empMapper;

	@Autowired
	private ProjectService projectService;
	@Autowired
	private EmpServiceImpl empServiceImpl;
	
	private static Logger logger = LoggerFactory.getLogger(CdDocShareContrloller.class);
	
	
	/**
	 * 得到登录用的UserNo 
	 * @return
	 */
	private static String getUserNo(){
		SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
		String userNo= sessionUser.getUserNo(); //得到登录用的UserNo 
		return userNo;
	}
	
	/*********************************我分享的********************************************/
	
	
	/**
	 * 查询当前登录用户分享的文件信息  要使用联合查询
	 * @return
	 */

	
	@RequestMapping(value="selectShareFile",method = RequestMethod.GET)
	@ResponseBody
	public List<CdDocShareBo> selectShareFile(){
		List<CdDocShareBo> list = Lists.newArrayList();
		list=cdDocShareService.selectShareFolder();
		return list;
	}
	/**********************************************************************************/
	/***************************************分享给我**************************************/
	
	
	/**
	 * 查询文件分享
	 * @param parentId 父级目录
	 * @param shareFolderUuid 分享根目录的uuid（为了方便进行权限控制，不需要递归进行权限查询）
	 * @return
	 * @throws AppException 
	 */
	@RequestMapping(value="queryForList",method = RequestMethod.GET)
	@ResponseBody
	public CdDocShareFolderResutlBo queryForList(String parentId,String shareFolderUuid) throws AppException{
		CdDocShareFolderResutlBo bo = new CdDocShareFolderResutlBo();
		List<CdDocFolderVo> folderList;

		if(!StringUtils.isEmpty(shareFolderUuid) && !StringUtils.isEmpty(shareFolderUuid)){		//查询下级目录
			CdDocFolderVo parentVo = cdDocFileService.getById(parentId);
			
			//判断上级parentId是否存在；
			if(parentVo==null || !CommonConstant.COMMON_NO.equals(parentVo.getIsDel())){
				throw new AppException("目录不存在，可能已经被删除");
			}
			
			//判断上级VO是否是共享目录shareUuid的下级目录
			if( shareFolderUuid.equals(parentId) 
					|| (parentVo.getFolderPath() != null && parentVo.getFolderPath().contains(shareFolderUuid))){
				//正常（为了代码便于理解一下，所以使用了else）
			}else{
				throw new AppException("目录访问错误，请到根目录重新进入");
			}
			
			//判断当前用户是否有权限查看分享根目录数据
	
			
			folderList = cdDocFileService.queryForListForParentId(parentId,CommonConstant.COMMON_NO);
			
			
			bo.setShareUuid(shareFolderUuid);
			bo.setCdDocFolderVo(parentVo);
		}else{	
			//查询全部列表
			
			//通过 getUserNo得到用户的id 在通过用户的id查询角色的id
			List<AuapREmpOrgRoleVo> auapREmpOrgRoleVos = empServiceImpl.queryForListEmpNo(getUserNo());
			//得到项目 有多个项目id
			List<ProjectVo> list = projectService.queryEmpNo(getUserNo());

			folderList = cdDocShareAuthService.queryForList(getUserNo(),auapREmpOrgRoleVos,list,getUserNo());
			
			
			bo.setShareUuid("");
		}
		
		bo.setChildrenList(folderList);
		
		return bo;
	}
	
	/**
	 * 取消分享
	 * @return
	 */
	@RequestMapping(value="deleteShareFile",method = RequestMethod.GET)
	@ResponseBody
	public String deleteShareFile(String uuid){
		
		String[] split = uuid.split(",");
		
		cdDocShareService.deleteShareFile(split);
		
		return JsonSuccess.factory("取消分享成功");
	}
	
	
	/*********************************修改分享方式************************************************/
	
	/**
	 * 查询文件分享的信息
	 * @param uuid
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "queryForListShare",method = RequestMethod.GET)
	@ResponseBody
	public CdDocShareBo queryForListShare(String uuid,String folderId){
		CdDocShareBo cdDocShareBo = new CdDocShareBo();
	
			//新增 和修改
			cdDocShareBo=cdDocShareService.addShare(uuid,getUserNo());
		
			//cdDocShareBo = cdDocShareService.queryForListShare(uuid,getUserNo());
			return cdDocShareBo;
	}
}
